package com.fulihui.wgj.admin.api.subscribereply;

import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.subscribereply.request.*;
import com.fulihui.wgj.admin.api.subscribereply.vo.SubscribeReplyVO;
import com.fulihui.wgj.admin.integration.SubscribeReplyClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-26 13:59
 */
@RestController
@RequestMapping("/subreply")
@Api(tags = "关注回复接口")
public class SubscribeReplyController extends BaseController {

    @Autowired
    private SubscribeReplyClient subscribeReplyClient;

    @ApiOperation(value = "默认关注回复列表")
    @PostMapping("/default")
    public Result<SubscribeReplyVO> defaultReply(HttpServletRequest request) {
        SubscribeReplyRequest replyReq = new SubscribeReplyRequest();
        replyReq.setAppId(getAppid(request));
        return subscribeReplyClient.queryDefaultReply(replyReq);
    }

    @ApiOperation(value = "特定关注回复列表")
    @PostMapping("/specific")
    public Result<List<SubscribeReplyVO>> specificReply(HttpServletRequest request) {
        SubscribeReplyRequest replyReq = new SubscribeReplyRequest();
        replyReq.setAppId(getAppid(request));
        return subscribeReplyClient.querySpecificReply(replyReq);
    }

    @ApiOperation(value = "保存关注回复")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid SubscribeReplySaveRequest request, BindingResult validResult,
                       HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return subscribeReplyClient.save(request);
    }

    @ApiOperation(value = "修改关注回复")
    @PostMapping("/update")
    public Result save(@RequestBody @Valid SubscribeReplyUpdateRequest request, BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeReplyClient.update(request);
    }

    @ApiOperation(value = "删除关注回复")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Valid SubscribeReplyDelRequest request, BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeReplyClient.delete(request.getId());
    }

    @ApiOperation(value = "打开或者关闭回复")
    @PostMapping("/offOrOn")
    public Result offOrOn(@RequestBody @Valid SubscribeReplySwitchRequest request, BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeReplyClient.enableOrDisabled(request.getId(), request.getUseful());
    }

    @ApiOperation(value = "预览")
    @PostMapping("preview")
    public Result<SubscribeReplyVO> preview(@RequestBody @Valid SubscribeReplyQueryRequest request, BindingResult validResult,
                                            HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return subscribeReplyClient.preview(request);
    }

    @ApiOperation(value = "发送到手机预览")
    @PostMapping("sendPre")
    public Result<Boolean> sendPre(@RequestBody @Valid SubscribeReplySendPreRequest request, BindingResult validResult,
                                   HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return subscribeReplyClient.sendPre(request);
    }

}
